package com.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.entity.Master;
import com.entity.Student;
import com.service.MasterService;
import com.service.StudentService;

@Controller
public class MasterController {
	@Autowired
	private MasterService ms;
	@Autowired
	private StudentService ss;

	/*
	 * 舍管登录
	 */
	@PostMapping(value = "masterLogin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject studentLogin(@RequestBody Master master, HttpSession session) {
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "登录失败";
		Master mas = ms.masterLogin(master.getMname(), master.getPassword());
		if (mas != null) {
			status = 0;
			msg = "登陆成功";
			json.put("status", status);
			json.put("msg", msg);
			// 登录后将用户存在session
			session.setAttribute("master", mas);
			json.put("mas", mas);
			return json;
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	/*
	 * 退出登录
	 */
	@PostMapping(value = "MasterLoginOut")
	@ResponseBody
	public JSONObject MasterLoginOut(@RequestBody HttpSession session) {
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "退出失败";

		try {
			// 销毁session
			session.invalidate();
			status = 0;
			msg = "退出成功";
		} catch (Exception e) {
			e.printStackTrace();
		}
		json.put("status", status);
		json.put("message", msg);
		return json;
	}

	/*
	 * 修改密码
	 */
	@GetMapping(value = "/updateMasterPassword", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateMasterPassword(@RequestBody HttpSession session, String sname, String password) {
		System.out.println("舍管密码" + password);
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "修改密码失败";
		if ("".equals(password)) {
			status = 1;
			msg = "密码不能为空";
			json.put("status", status);
			json.put("msg", msg);
			return json;
		} else if (ms.updateMasterPassword(sname, password)) {
			System.out.println(sname);
			System.out.println(password);
			status = 0;
			msg = "修改成功";
			json.put("status", status);
			json.put("msg", msg);
			return json;
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	/*
	 * 舍管查询个人信息
	 */

	@PostMapping(value = "/selectMasterByMname", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject selectMasterByMname(@RequestBody HttpSession session, String mname) {
		JSONObject json = new JSONObject();
		Student student = json.parseObject(mname, Master.class);
		int status = 0;
		String msg = "查询成功";
		Master mas = null;
		try {
			mas = ms.selectMasterByMname(mname);
			System.out.println(mas);
		} catch (Exception e) {
			e.printStackTrace();
			status = 0;
			msg = "操作失败，请稍后再试";
		}
		json.put("mas", mas);
		json.put("status", status);
		json.put("message", msg);
		return json;

	}

	/*
	 * 修改个人信息
	 */
	@PostMapping(value = "/updateMaster", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateMaster(@RequestBody Master master) {
		JSONObject json = new JSONObject();
		int status = 0;
		String msg = "修改成功";
		boolean result = ms.updateMaster(master);
		json.put("result", result);
		json.put("status", status);
		json.put("message", msg);
		return json;

	}

	/*
	 * 添加学生
	 */
	@PostMapping(value = "addStudent", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject addStudent(@RequestBody Student student) {
		JSONObject json = new JSONObject();
		if (student.getSname() == null || student.getSname().equals("")) {
			json.put("status", -1);
			json.put("msg", "名字不能为空");
			return json;
		} else if (student.getPassword() == null || student.getPassword().equals("")) {
			json.put("status", -1);
			json.put("msg", "密码不能为空");
			return json;
		} else if (student.getSno() == null || student.getSno().equals("")) {
			json.put("status", -1);
			json.put("msg", "学号不能为空");
			return json;
		} /*
			 * else if (student.getSex() == null || student.getSex().equals(""))
			 * { json.put("status", -1); json.put("msg", "性别不能为空"); return json;
			 * } else if (student.getPhone() == null ||
			 * student.getPhone().equals("")) { json.put("status", -1);
			 * json.put("msg", "电话号码不能为空"); return json; } else if
			 * (student.getSchool() == null || student.getSchool().equals("")) {
			 * json.put("status", -1); json.put("msg", "学校不能为空"); return json; }
			 * else if (student.getEducation() == null ||
			 * student.getEducation().equals("")) { json.put("status", -1);
			 * json.put("msg", "学历不能为空"); return json; } else if
			 * (student.getMajor() == null || student.getMajor().equals("")) {
			 * json.put("status", -1); json.put("msg", "专业不能为空"); return json; }
			 * else if (student.getGrade() == null ||
			 * student.getGrade().equals("")) { json.put("status", -1);
			 * json.put("msg", "年级不能为空"); return json; } else if
			 * (student.getSclass() == null || student.getSclass().equals("")) {
			 * json.put("status", -1); json.put("msg", "班级不能为空"); return json; }
			 * else if (student.getDormitory() == null ||
			 * student.getDormitory().equals("")) { json.put("status", -1);
			 * json.put("msg", "宿舍楼不能为空"); return json; } else if
			 * (student.getDno() == null || student.getDno().equals("")) {
			 * json.put("status", -1); json.put("msg", "宿舍号"); return json; }
			 */else {
			ss.addStudent(student);
			json.put("status", 0);
			json.put("msg", "添加成功");
		}
		json.put("status", 0);
		json.put("msg", "添加成功");
		return json;
	}

	/*
	 * 删除学生
	 */
	@PostMapping(value = "/deleteStudentBySno", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject deleteStudentBySno(@RequestBody Student student) {
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "操作失败";
		int b = ss.deleteStudentBySno(student.getSno());
		// json.put("status", -1);
		// json.put("msg", "操作失败，请稍后再试");
		// return json;

		if (b >= 0) {
			status = 0;
			msg = "操作成功";
			json.put("status", status);
			json.put("msg", msg);
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;
	}

	/*
	 * 修改学生宿舍号
	 */
	@PostMapping(value = "/updateStudentDnoBySno", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateStudentDnoBySno(@RequestBody Student student, HttpSession session) {
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "修改失败";
		if ("".equals(student.getSno()) || student.getSno() == null) {
			status = 1;
			msg = "宿舍号不能为空";
			json.put("status", status);
			json.put("msg", msg);
			return json;
		} else if (ss.updateStudentDnoBySno(student.getSno(), student.getDno())) {
			status = 0;
			msg = "修改成功";
			json.put("status", status);
			json.put("msg", msg);
			return json;
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	/*
	 * 根据宿舍号查询宿舍成员
	 */
	@PostMapping(value = "/findStudentByDno", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject findStudentByDno(@RequestBody Student student) {
		JSONObject json = new JSONObject();
		if (student.getDno() == null || "".equals(student.getDno())) {
			int status1 = -1;
			String msg1 = "查询失败";
			json.put("status1", status1);
			json.put("msg1", msg1);
		} else {
			List<Student> stu=  ss.findStudentByDno(student.getDno());
			int status = 0;
			String msg = "查询成功";
			json.put("stu", stu);
			json.put("status", status);
			json.put("msg", msg);
			return json;
		}
		return json;

	}
}
